package com.atguigu.sink;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.DateTimeBucketAssigner;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;
import java.time.ZoneId;

public class FileSinkDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 必须开启checkpoint，否则一直都是 .inprogress
        env.enableCheckpointing(200, CheckpointingMode.EXACTLY_ONCE);

        // TODO 每个目录中，都有 并行度个数的 文件在写入
        env.setParallelism(2);
        DataGeneratorSource<String> source = new DataGeneratorSource<>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long aLong) throws Exception {
                        return "number:" + aLong;
                    }
                },
                Integer.MAX_VALUE,
                RateLimiterStrategy.perSecond(50),
                Types.STRING
        );


        DataStreamSource<String> datagensource = env.fromSource(source, WatermarkStrategy.noWatermarks(), "datagensource");

        FileSink<String> fileSink = FileSink
                // 输出行式存储的文件，指定路径、指定编码
                .forRowFormat(new Path("D:\\tmp\\out"), new SimpleStringEncoder<String>("UTF-8"))
                // 输出文件的一些配置： 文件名的前缀、后缀
                .withOutputFileConfig(OutputFileConfig.builder()
                        .withPartPrefix("flink-")
                        .withPartSuffix(".log")
                        .build())
                // 按照目录分桶：如下，就是每个小时一个目录
                .withBucketAssigner(new DateTimeBucketAssigner<>("yyyy-MM-dd HH", ZoneId.systemDefault()))
                // 文件滚动策略:  1分钟 或 1m
                .withRollingPolicy(DefaultRollingPolicy.builder()
                        .withRolloverInterval(Duration.ofSeconds(5))
                        .withMaxPartSize(new MemorySize(1024 * 5))
                        .build())
                .build();

        datagensource.sinkTo(fileSink);

        env.execute();
    }
}
